<?php

class LocationController extends ControllerModuleBackup
{
	public function actionIndex()
	{
		$backupLocationsAddForm = new BackupLocationsAddForm();
		$backupLocation = new BackupLocations();
		$now = time();
		
		$postData = app()->request->getPost('BackupLocationsAddForm', null);
		if($postData){
			$backupLocationsAddForm->attributes = $postData;
			$ret = false;
			$detail = $errors = null;
			if($backupLocationsAddForm->protocol != BackupLocations::PROTOCOL_HOMEDIR){
				$backupLocationsAddForm->setScenario('not_homedir');
			}
			if($backupLocationsAddForm->validate()){
				$backupLocation->attributes = $postData;
				if($backupLocation->protocol != BackupLocations::PROTOCOL_HOMEDIR){
					$backupLocation->setScenario('not_homedir');
				}else{
					$backupLocation->server = '';
					$backupLocation->username = '';
					$backupLocation->password = '';
					$backupLocation->port = '';
					$backupLocation->directory = '';
				}
				$ret = $backupLocation->save();
				$detail = $backupLocation->attributes;
				if(!$ret){
					$errors = $backupLocation->getErrors();
				}
			}else{
				$errors = $backupLocationsAddForm->getErrors();
			}
			
			$this->_returnAjax(
				array(
					'result' => $ret,
					'detail' => $detail,
					'errors' => $errors
				)
			);
		}
		
		$this->render('index', array(
			'backupLocationsAddForm' => $backupLocationsAddForm,
			'backupLocationsEditForm' => new BackupLocationsEditForm(),
			'sort' => app()->request->getParam('BackupLocations_sort', 'name_sort'),
			'backupLocation' => $backupLocation
		));
	}
	
	public function actionDetail($id_backup_locations)
	{
		$id_backup_locations = app()->request->getParam('id_backup_locations', null);
		if($id_backup_locations){
			if(app()->request->isAjaxRequest){
				$backupLocation = $this->loadModelByPk($id_backup_locations, 'BackupLocations');
				$this->_returnAjax(
					$backupLocation->attributes
				);
			}
		}
	}
	
	public function actionEdit()
    {
		if(app()->request->isAjaxRequest){
	        $postData = app()->request->getParam('BackupLocationsEditForm', null);
	    	if($postData){
	    		$backupLocationsEditForm = new BackupLocationsEditForm();
	    		$backupLocationsEditForm->attributes = $postData;
				$ret = false;
				$detail = $errors = null;
				$backupLocation = $this->loadModelByPk($postData['id_backup_locations'], 'BackupLocations');
				
		    	if($backupLocationsEditForm->protocol != BackupLocations::PROTOCOL_HOMEDIR){
					$backupLocationsEditForm->setScenario('not_homedir');
				}
		    	if($backupLocationsEditForm->validate()){
					$backupLocation->attributes = $postData;
			    	if($backupLocation->protocol != BackupLocations::PROTOCOL_HOMEDIR){
						$backupLocation->setScenario('not_homedir');
					}else{
						$backupLocation->server = '';
						$backupLocation->username = '';
						$backupLocation->password = '';
						$backupLocation->port = '';
						$backupLocation->directory = '';
					}
					$ret = $backupLocation->save();
					$detail = $backupLocation->attributes;
					if(!$ret){
						$errors = $backupLocation->getErrors();
					}
				}else{
					$errors = $backupLocationsEditForm->getErrors();
				}
				
            	$this->_returnAjax(array(
            		'result' => $ret,
					'detail' => $detail,
					'errors' => $errors
				));
			}
		}else{
			$user->save();	
			$this->render('edit', array(
				'user' => $user,
			)); 
		}
    }
	
	public function actionDelete()
	{
		if(app()->request->isAjaxRequest){
			$id_backup_locations = app()->request->getPost('id_backup_locations', null);
			if($id_backup_locations){
				$backupLocation = $this->loadModelByPk($id_backup_locations, 'BackupLocations');
				
				$this->_returnAjax(array(
					'result' => $backupLocation->delete(),
					'errors' => $backupLocation->getErrors()
				));
			}
		
			$this->_end();
		}
	}
	
	public function accessRules()
	{
		return array(
			array(
				'allow',
				'actions' => array('index', 'delete', 'detail', 'edit'),
				'expression' => 'app()->user->isLogin() && app()->user->hasManageBackupPerm()',
				'redirect' => array('default/noaccess')
			),

			array(
				'deny',	//should be 'deny', 'allow' is used to give convenient to make test
				'users' => array('*'),
				'redirect' => array('user/login')
			)
		);
	}
}